package com.mogujie.tt.app;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.mogujie.tt.DB.sp.LoginSp;
import com.mogujie.tt.R;
import com.mogujie.tt.config.RegisterConstant;
import com.mogujie.tt.config.SysConstant;
import com.mogujie.tt.imservice.service.IMService;
import com.mogujie.tt.utils.FileUtil;
import com.mogujie.tt.utils.ImageLoaderUtil;
import com.mogujie.tt.utils.LockPatternUtils;
import com.mogujie.tt.utils.Logger;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;
import java.io.File;


public class IMApplication extends Application {

	private Logger logger = Logger.getLogger(IMApplication.class);
	//单例
	private static IMApplication inst = new IMApplication();
	public static IMApplication instance() {
		return inst;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}

	@Override
	public void onCreate() {
		super.onCreate();
		logger.i("Application starts");
		Log.v("IMApplication", "Application starts...........");
		Configuration config = getResources().getConfiguration();
		int smallestScreenWidth = config.smallestScreenWidthDp;
		Log.v("IMApplication", "smallestScreenWidth = "+smallestScreenWidth);
//		RegisterConstant.APPLICATION_PATH = getApplicationContext().getFilesDir().getAbsolutePath();
//		Log.v("IMApplication", "APPLICATION_PATH = " + RegisterConstant.APPLICATION_PATH);
		File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
	//	Log.v("IMApplication", "path = " + path.toString());
		String printTxtPath = getApplicationContext().getFilesDir().getAbsolutePath();
		RegisterConstant.APP_FILE_PATH = printTxtPath+"/";
	//	Log.v("IMApplication","path = "+printTxtPath);

		RegisterConstant.PHOTO_PATH =path.toString()+"/";
		startIMService();//开启服务
		ImageLoaderUtil.initImageLoaderConfig(getApplicationContext());


		mInstance = this;

		resultLock();
		ImagelodeView(); //图片缓存初始化
	}

	private void ImagelodeView() {
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() //
				.showImageForEmptyUri(R.drawable.ic_launcher) //
				.showImageOnFail(R.drawable.ic_launcher) //
				.cacheInMemory(true) //
				.cacheOnDisk(true) //
				.build();//
		ImageLoaderConfiguration config = new ImageLoaderConfiguration//
				.Builder(getApplicationContext())//
				.defaultDisplayImageOptions(defaultOptions)//
				.discCacheSize(50 * 1024 * 1024)//
				.discCacheFileCount(50)// 缓存一百张图片
				.writeDebugLogs()//
				.build();//
		ImageLoader.getInstance().init(config);
	}

	public void resultLock(){
		LoginSp.instance().init(this);
		mLockPatternUtils = new LockPatternUtils(this,LoginSp.instance().getLockLoginId());
	}

	private void startIMService() {
		logger.i("start IMService");
		Intent intent = new Intent();
		intent.setClass(this, IMService.class);
		startService(intent);
	}

    public static boolean gifRunning = true;//gif是否运行
	private static IMApplication mInstance;
	private LockPatternUtils mLockPatternUtils;
	public boolean isActive = true;

	public static IMApplication getInstance() {
		return mInstance;
	}

	public LockPatternUtils getLockPatternUtils() {
		return mLockPatternUtils;
	}

	public boolean isAppOnForeground() {

		ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();

		List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();

		if (appProcesses == null)
			return false;

		for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}

		return false;
	}

}
